package com.mj.app.health.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.mj.app.common_util.picasso.ImageLoader;
import com.mj.app.health.MyApplication;
import com.mj.app.health.R;
import com.mj.app.health.activity.WebNormalActivity;
import com.mj.app.health.adapter.ServiceAdapter;
import com.mj.app.health.base.BaseFragment;
import com.mj.app.health.bean.AdBean;
import com.mj.app.health.bean.ServiceBean;
import com.mj.app.health.customer.ListViewNoScroll;
import com.mj.app.health.defination.IntentKeys;
import com.mj.app.health.net.HttpGo;
import com.mj.app.health.net.HttpListener;
import com.mj.app.health.net.response.MainResponse;
import com.mj.app.health.util.SharedPreferanceUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.BannerImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ========================================
 * *
 * title: AdditionFragment
 * description:
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public class AdditionFragment extends BaseFragment {

    @BindView(R.id.a_banner)
    Banner mainBanner;
    @BindView(R.id.a_lv)
    ListViewNoScroll aLv;

    private ServiceAdapter adapter;
    private List<ServiceBean> serviceBeans = new ArrayList<>();
    private  List<AdBean> ads = new ArrayList<>();

    private Handler       mHandler;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_addition;
    }

    @Override
    protected void init() {

        aLv.setAdapter(adapter = new ServiceAdapter(getActivity()));

        aLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle b = new Bundle();
                b.putString(IntentKeys.WEB_TITLE,serviceBeans.get(position).getPaperTitle());
                b.putString(IntentKeys.WEB_CONTENT,serviceBeans.get(position).getPaperContent());
                redirectTo(WebNormalActivity.class, false, b);
            }
        });

        mHandler = new Handler() {
            @Override
            public void handleMessage(final Message msg) {

                if(msg.what == 1){
                    initBanner(ads);
                    adapter.setServiceBeans(serviceBeans);
                }else{

                }
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {

                final Message msg = new Message();

                HttpGo.loadMain(SharedPreferanceUtil.getSp("UserId", ""), 0, 10, new HttpListener<MainResponse>() {
                    @Override
                    public void onSuccess(MainResponse mainResponse) {

                        ads = mainResponse.getData().getAd();
                        serviceBeans = mainResponse.getData().getPaper();

                        msg.what = 1;
                        mHandler.sendMessage(msg);
                    }

                    @Override
                    public void onError(String exception) {

                        msg.what = 0;
                        mHandler.sendMessage(msg);
                    }
                });
            }
        }).start();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initBanner(List<AdBean> ads) {

        final List<String> urlList = new ArrayList<>();

        for(AdBean ad: ads){
            urlList.add(ad.getAdImg());
        }
            mainBanner.setImages(urlList);
            mainBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            mainBanner.setImageLoader(new BannerImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    ImageLoader.load(path.toString(), imageView, R.drawable.bg_empty);
                }
            });
            mainBanner.getLayoutParams().height = (int) (MyApplication.getScreenWidth() * 0.5);
            mainBanner.start();
            mainBanner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {

                    // 处理点击广告事件
                }
            });
//        }
    }
}
